<template>
  <!-- 选择医生 -->
  <el-dialog title="选择医生" :visible="value" width="900px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <div class="form-tool">
      <div>&nbsp;</div>
      <div>
        <el-form :inline="true" @submit.native.prevent="getData(-1)">
          <el-form-item>
            <!-- <el-input v-model="form.doctorId" placeholder="医生姓名"></el-input> -->
            <searchDoctor v-model="form.doctorId" style="width:300px;"></searchDoctor>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" native-type="submit" circle></el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <table class="table-box">
      <thead>
        <tr>
          <th></th>
          <th>姓名</th>
          <th>性别</th>
          <th>职称</th>
          <th>已分诊</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in tableList" :key="index" @click="onClickTr(item,index)">
          <td>
            <img v-if="item.head" :src="item.head|imageUrl" alt="" class="head-img" style="width:40px;height:40px;">
            <img v-else src="@/img/unknown-avatar.png" class="head-img" style="width:40px;height:40px;" alt="">
          </td>
          <td>{{item.trueName}}</td>
          <td>{{item.sex|toSex}}</td>
          <td><div>{{item.doctorTitle|changeAssist(doctorTitleArray)}}</div></td>
          <td>
            <span v-for="(item,index) in item.patientInfoList" :key="index" style="margin-right:10px;">{{item.name}}</span>
          </td>
          <td>
            <div class="form-tool" style=" align-items: center;"> 
              <div style="width:50px;">
                <i v-if="index==trIndex" class="el-icon-success primary-color" style="font-size:25px;"></i>
              </div>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
    <br>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br>
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
    <reserveTime v-model="dialogTime" :deptId="form.deptId" :doctorId="curDoctor.id" type="dj" @confirm="onConfirmTime"></reserveTime>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "curItem"],
  components: {
    reserveTime: () => import("../../_com/reserveTime.vue"),
    searchDoctor:()=>import("@/pages/_com/newSearchDoctor.vue")
  },
  data() {
    return {
      dialogTime: false,
      btnLoading: false,
      pager: { pageSize: 10, pageNum: 1, total: 0 },
      tableList: [],
      form: { 
        hpId: "",
        deptId: "",
        dateDay: ""
      },
      trIndex: -1,
      curDoctor: {},
      //职称
      doctorTitleArray: []
    };
  },
  mounted() {
    let th = this;
    th.form.hpId = th.adminUser.hpId;
    th.form.dateDay = th.$utils.FormatTime("yyyy-MM-dd", new Date().getTime());
    th.getCommonConfigByKey("医生职称").then(res => {
      th.doctorTitleArray = res;
    });
  },
  methods: {
    onSubmit() {
      let th = this;
      if (th.trIndex == -1) {
        th.$message.warning("请选择医生");
        return;
      }
      th.dialogTime = true;
    },
    //选择医生
    onClickTr(obj, index) {
      let th = this;
      th.trIndex = index;
      th.curDoctor = obj;
    },
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      let j = {};
      Object.assign(j, th.form, th.pager);
      th.$api.getDoctorListByDate(j).then(res => {
        if (res.executed) {
          th.tableList = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
      });
    },
    //确定预约时间
    onConfirmTime(obj) {
      let th = this;
      let j = {
        registerId: th.curItem.id,
        doctorId: th.curDoctor.id,
        dateDay: obj.dateStr,
        timeId: obj.id,
        deptId: th.form.deptId
      };
      th.btnLoading = true;
      th.$api.registerTriage(j).then(res => {
        if (res.executed) {
          th.$message.success("操作完成");
          th.$emit("success");
          th.onClose();
        } else {
          th.$message.error(res.message);
        }
        th.btnLoading = false;
      });
    },
    onOpen() {
      let th = this;
      th.form.deptId = th.curItem.deptId;
      th.$nextTick(function() {
        th.getData();
      });
    },
    onClose() {
      let th = this;
      th.tableList = [];
      th.trIndex = -1;
      th.$emit("input", false);
    }
  }
};
</script>

